package com.zhai.boot.controller;

import com.zhai.boot.common.ServiceResult;
import com.zhai.boot.common.ServiceResultConstant;
import com.zhai.boot.domain.in.ZhaiSaveIn;
import com.zhai.boot.service.ZhaiService;
import com.zhai.boot.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @package: com.zhai.boot.controller
 * @project-name: zhai-boot
 * @description: zhai controller
 * @author: Created by 翟凤玺
 * @create-datetime: 2018-8-7 14:05
 */
@RestController
@RequestMapping("/zhai")
public class ZhaiController {

    @Autowired
    private ZhaiService zhaiService;

    @GetMapping("/get/{id}")
    public ResponseEntity getById(@PathVariable("id") String id) {
        if (ObjectUtil.isNull(id)) {
            return ResponseEntity.ok(ServiceResult.failure(ServiceResultConstant.SYS_NOT_NULL));
        }
        ServiceResult<Object> result = zhaiService.getById(id);
        return ResponseEntity.ok().body(ServiceResult.defined(result));
    }

    @PostMapping("/save")
    public ResponseEntity save(@RequestBody @Validated ZhaiSaveIn in) {
        if (ObjectUtil.isNull(in)) {
            return ResponseEntity.ok(ServiceResult.failure(ServiceResultConstant.SYS_NOT_NULL));
        }
        ServiceResult<Object> result = zhaiService.save(in);
        return ResponseEntity.ok().body(ServiceResult.defined(result));
    }
}
